# -*- coding:utf-8 _*-

from lxml import etree
from spider import parse
from Data.Read_and_Write import Redis_Push


def get_cate(kw):
    try:
        # 一级目录请求
        one_cate_response = parse.get('http://b2b.huangye88.com/{}'.format(kw))
        if one_cate_response is not None:
            one_cate_html = etree.HTML(one_cate_response)
            one_cate_list = one_cate_html.xpath('//div[@class="tag_tx"]/ul/li/a')
            for one_cate in one_cate_list:
                one_cate_title = one_cate.xpath('./text()')[0] if len(one_cate.xpath('./text()')) != 0 else None
                one_cate_href = one_cate.xpath('./@href')[0] if len(one_cate.xpath('./@href')) != 0 else None
                # 二级目录请求
                try:
                    if one_cate_href is not None and one_cate_title is not None:
                        two_cate_response = parse.get(one_cate_href)
                        if two_cate_response is not None:
                            two_cate_html = etree.HTML(two_cate_response)
                            two_cate_list = two_cate_html.xpath('//ul[@class="cats_list qiyelist_cats_list"]/li/a')
                            for two_cate in two_cate_list:
                                two_cate_title = two_cate.xpath('./text()')[0] if len(
                                    two_cate.xpath('./text()')) != 0 else None
                                two_cate_href = two_cate.xpath('./@href')[0] if len(
                                    two_cate.xpath('./@href')) != 0 else None
                                if two_cate_href is not None and two_cate_title is not None:  # xpath能取到值，表示存在二级目录
                                    vaule = '{"Cate_one":"%s","Cate_two":"%s","Cate_two_url":"%s"}' % (
                                    one_cate_title, two_cate_title, two_cate_href)
                                    print('Categray:', 'Cate_one:{};'.format(one_cate_title),
                                          'Cate_two:{};'.format(two_cate_title),
                                          'Cate_two_url:{}'.format(two_cate_href))
                                    Redis_Push(DbName=kw, Vaule=vaule)

                                else:
                                    vaule = '{"Cate_one":"%s","Cate_two":"%s","Cate_two_url":"%s"}' % (
                                        one_cate_title, one_cate_title, one_cate_href)
                                    print('Categray:''Cate_one:{};'.format(one_cate_title),
                                          'Cate_two:{};'.format(one_cate_title),
                                          'Cate_two_url:{}'.format(one_cate_href))
                                    Redis_Push(DbName=kw, Vaule=vaule)
                except:
                    pass
    except:
        pass


if __name__ == '__main__':
    get_cate()
